import Taro from '@tarojs/taro';
import aepApiRequest, { cozeApiRequest } from '../utils/aepApiRequest';

export async function bindDevice(params: IDevice) {
  const { homeId } = params;
  return aepApiRequest(`app/smartHome/v2/homes/${homeId}/addDevice`, {
    method: 'POST',
    body: params,
  });
}

export async function unbindDevice(params: { sno: string; homeId: number }) {
  const { homeId, sno } = params;
  return aepApiRequest(`app/smartHome/homes/${homeId}/deleteDevice`, {
    method: 'DELETE',
    body: sno,
  });
}

export async function unbindDeviceByMac(params: { deviceSourceType: number, productKey: string, mac: string; homeId: number, id: string }) {
  const { homeId, deviceSourceType, productKey, mac, id } = params;
  const uid = Taro.getStorageSync('uid')
  return cozeApiRequest(`v3/users/${uid}/devices/${id}`, {
    method: 'DELETE',
    body: { deviceSourceType, productKey, mac },
  });
}

export async function getUserDeviceList() {
  const uid = Taro.getStorageSync('uid')
  const path = `v3/users/${uid}/devices`
  return cozeApiRequest(path, {
    method: 'GET',
  });
}

export async function bindQrcodeDevice(params: {
  homeId: number;
  content: string;
}) {
  const { homeId, content } = params;
  return aepApiRequest(`app/smartHome/v2/homes/${homeId}/addDeviceByQrCode`, {
    method: 'POST',
    body: content,
  });
}

export async function analysisQrcodeDevice(params: {
  content: string;
}) {
  const { content } = params;
  return aepApiRequest(`/app/smartHome/v2/device_info_by_ciphertext?ciphertext=${encodeURIComponent(content)}`, {
    method: 'GET',
    body: {},
  });
}

export async function editDeviceName({
  id,
  name,
  remark,
}: IDevice) {
  const params = {
    name,
    remark,
  };
  const uid = Taro.getStorageSync('uid')
  return cozeApiRequest(`v3/users/${uid}/devices/${id}`, {
    method: 'PUT',
    body: params,
  });
}

export async function hideDevices({
  hide,
  snos,
}: {
  hide: boolean;
  snos: string[];
}) {
  const params = { hide, snos };
  return aepApiRequest(
    'app/smart_home/devices/hide',
    {
      method: 'PUT',
      body: params,
    },
    true
  );
}

export async function getWechatTicket({ modelId, mac, productKey }: { modelId: string, mac: string, productKey: string }) {
  return aepApiRequest(`app/applets/getSnTicket?modelId=${modelId}&sn=${mac}&productKey=${productKey}`, {
    method: 'GET',
  });
}
